<template>
  <div v-loading="taskBaseLoading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('任务名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('任务名称')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('创建人')" prop="createUserId">
            <UserSelect
              v-model="form.createUserId"
              :placeholder="this.$t('选择用户')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开始时间')" prop="startDate">
            <DatePicker v-model="form.startDate" :placeholder="$t('开始时间')" @change="checkTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('截止时间')" prop="endDate">
            <DatePicker v-model="form.endDate" :placeholder="$t('截止时间')" @change="checkTime" />
          </el-form-item>
        </el-col>
        <div v-if="isShowMainBody">
          <el-col :span="12">
            <el-form-item :label="$t('主体类型')" prop="businessType">
              <dictSelect
                v-model="form.businessType"
                :clearable="true"
                dictionary="contract_type"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" prop="businessId">
            <el-form-item :label="$t('主体名称')" prop="businessId">
              <MainBodySelect
                v-model="form.businessId"
                :parameters="form.businessType"
              />
            </el-form-item>
          </el-col>
        </div>
        <el-col :span="12">
          <el-form-item :label="$t('参与人员')" prop="taskParticipantArray">
            <UserSelect
              v-model="form.taskParticipantArray"
              :multiple="true"
              :placeholder="this.$t('选择用户')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :maxlength="2000" :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="isSubmitShow" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="deal:taskbase" />
    </div>
  </div>
</template>

<script>
    import MainBodySelect from '@/components/MainBodySelect'
    import { saveTaskBase, getTaskBase } from '@/api/deal/taskBase/taskBase'
    export default {
        components: {
          MainBodySelect
        },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                taskBaseLoading: false,
                // 表单校验
                rules: {
                  name: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  createUserId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  startDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  endDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  businessId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  businessType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 主体是否显示
                isShowMainBody: false,
                // 操作阿牛
                isSubmitShow: true
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.taskBaseLoading = true
                if (this.$route.query.id === null || this.$route.query.id === undefined || this.$route.query.id === '') {
                  this.isShowMainBody = true
                }
                if (data != null && data.id !== undefined) {
                    getTaskBase(data.id).then(response => {
                        this.form = response.data
                    })
                  this.taskBaseLoading = false
                } else {
                    this.reset()
                    try {
                      if (this.$route.query.id !== null && this.$route.query.id !== undefined && this.$route.query.id !== '') {
                        var arr = this.$route.fullPath.split('/')
                        this.form.businessType = arr[1]
                      }
                    } catch (e) {
                      console.log(e)
                    }
                    this.form.businessId = this.$route.query.id
                    this.taskBaseLoading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                createUserId: this.$store.state.user.userId,
                businessId: this.$route.query.id,
                businessType: undefined,
                name: undefined,
                startDate: undefined,
                endDate: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                taskParticipant: undefined,
                taskParticipantList: [],
                taskParticipantArray: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.taskBaseLoading = false
                try {
                  this.$parent.$parent.$parent.taskBaseLoading = false
                  this.$parent.$parent.reset()
                } catch (e) {
                  console.log(this)
                }
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.taskBaseLoading = false
                this.$emit('closeTaskBaseLoading')
                // 关闭form页面
                this.$parent.$parent.taskBaseLoading = false
                try {
                  this.$parent.$parent.getList()
                } catch (e) {
                  console.log(e)
                }
                // 刷新list页面
                try {
                  this.$parent.$parent.$parent.getList()
                } catch (e) {
                  console.log(e)
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.taskBaseLoading = true
                        saveTaskBase(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                              this.taskBaseLoading = false
                            }
                        }).catch(() => {
                          this.taskBaseLoading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
            },
            // 开始时间结束时间校验
            checkTime() {
              var start = new Date(this.form.startDate).getTime()
              var end = new Date(this.form.endDate).getTime()
              if (start > end) {
                this.msgError(this.$t('开始时间不能大于结束时间'))
                this.form.endDate = undefined
              }
            }

        }

    }
</script>
